//  调用 
checklogin()

//  用户名渲染 函数调用
renderUsername()

//  退出登录
logout()

//  首页统计数据
render()

//  首页统计数据
async function render() {

  // const token = localStorage.getItem('token')
  const res = await axios({
    url: '/dashboard',
  })
  const { overview, year, salaryData, groupData, provinceData } = res.data.data
  // 就业薪资数据函数
  renderoverview(overview)
  // 渲染薪资走势函数
  renderYear(year)
  // 渲染薪资走势 饼图
  rendersalaryData(salaryData)
  // 班级每组薪资groupData
  rendergroupData(groupData)
  //  男女薪资分布 环图标
  rendersalaryDatas(salaryData)
  //  籍贯分布
  renderProvince(provinceData)
}

//  就业薪资数据 
function renderoverview(overview) {
  Object.keys(overview).forEach(key => {
    document.querySelector(`.${key}`).innerHTML = overview[key]
  })
}


//  渲染薪资走势折线图
function renderYear(year) {
  // console.log(year);
  const myChart = echarts.init(document.querySelector('#line'));
  const option = {
    tooltip: {
      // show: true 默认是显示
      trigger: 'axis'
    },
    title: {
      text: '2022全学科薪资走势',
      left: 10,
      top: 10
    },
    grid: {
      top: 80
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: year.map(ele => ele.month),
      // xAxis.axisLine.lineStyle.color 
      axisLine: {
        lineStyle: {
          color: '#b1b1b1',
          type: 'dashed'
        }
      }
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'  //  虚线
        },
      },
    },
    series: [
      {
        data: year.map(ele => ele.salary),
        symbolSize: 15, //  圆圈大小
        type: 'line',
        lineStyle: {
          width: '10', //  折线粗细
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 1,
            y2: 0,
            colorStops: [
              {
                offset: 0,
                color: '#51aeff' // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#5d76f0' // 100% 处的颜色
              }
            ],
            global: false // 缺省为 false
          },
        },
        //阴影颜色。支持的格式同color
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: '#b7ddfe' // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#fff' // 100% 处的颜色
              }
            ],
            global: false // 缺省为 false
          },
        },
        //圆润折线
        smooth: true,
      }
    ]
  };

  myChart.setOption(option);
}

//  渲染薪资走势 饼图
function rendersalaryData(salaryData) {
  // console.log(salaryData);
  const myChart = echarts.init(document.querySelector('#salary'));
  const option = {
    color: ['#fda224', '#5097ff', '#3cc4fc', '#37e1a4'],
    title: {
      text: '班级薪资分布',
      left: '10',
      top: '8'
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: '5%',
      left: 'center'
    },
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        radius: ['55%', '70%'],
        avoidLabelOverlap: true,
        itemStyle: {
          borderRadius: 40,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: true,
          position: 'outside'  // 标签引导线
        },
        emphasis: {
          // label: {
          //   show: true,
          //   fontSize: 40,
          //   fontWeight: 'bold'
          // }
        },
        labelLine: {
          show: true
        },
        // data: salaryData.map(ele => ele.label)
        data: salaryData.map(ele => {
          const { label, g_count, b_count } = ele
          return { value: g_count + b_count, name: label }
        }),
      }
    ]
  };

  myChart.setOption(option);
}


//  班级每组薪资groupData
function rendergroupData(groupData) {
  // console.log(groupData[1]);
  var myChart = echarts.init(document.querySelector('#lines'));
  option = {
    tooltip: {},
    grid: {
      left: 70,
      top: 30,
      right: 30,
      bottom: 50
    },
    xAxis: {
      type: 'category',
      data: groupData[1].map(ele => ele.name),
      axisLine: {
        type: 'dashed',
        color: '#bdbdbd'
      }
    },
    yAxis: {
      type: 'value',
      splitLine: {
        type: 'dashed'
      }
    },
    series: [
      {
        name: '期望薪资',
        data: groupData[1].map(ele => ele.hope_salary),
        type: 'bar',
        itemStyle: {
          //  渐变色
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#34D39A' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(52,211,154,0.2)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
      },
      {
        name: '就业薪资',
        data: groupData[1].map(ele => ele.salary),
        type: 'bar',
        itemStyle: {
          //  渐变色
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#499FEE' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(73,159,238,0.2)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
      }
    ]
  };

  myChart.setOption(option);

  document.querySelector('#btns').addEventListener('click', function (e) {
    if (e.target.classList.contains('btn')) {
      document.querySelector('.btn-blue').classList.remove('btn-blue')
      e.target.classList.add('btn-blue')
      //  获取 点击的元素
      const index = e.target.innerText
      // console.log(index)
      option.xAxis.data = groupData[index].map(ele => ele.name)
      option.series[0].data = groupData[index].map(ele => ele.hope_salary)
      option.series[1].data = groupData[index].map(ele => ele.hope_salary)
      myChart.setOption(option)
    }
  })
}


//  男女薪资分布 环图标
function rendersalaryDatas(salaryData) {
  console.log(salaryData);
  const myChart = echarts.init(document.querySelector('#gender'));
  const option = {
    title: [
      {
        text: '男女薪资分布',
        top: '15',
        left: '15',
      },
      {
        text: '男生',
        top: '45%',
        left: '45%',
        textStyle: {
          fontSize: 16,
        }
      },
      {
        text: '女生',
        top: '85%',
        left: '45%',
        textStyle: {
          fontSize: 16,
        }
      },
    ],
    tooltip: {
      trigger: 'item'
    },
    series: [
      {
        type: 'pie',
        radius: 50,
        radius: ['20%', '30%'],
        center: ['50%', '30%'],
        datasetIndex: 1,
        data: salaryData.map(ele => {
          const { label, b_count } = ele
          return { name: label, value: b_count }
        })
      },
      {
        type: 'pie',
        radius: 50,
        radius: ['20%', '30%'],
        center: ['50%', '70%'],
        datasetIndex: 2,
        data: salaryData.map(ele => {
          const { label, g_count } = ele
          return { name: label, value: g_count }
        })
      },

    ]
  };

  myChart.setOption(option);

}

//  籍贯分布
  function renderProvince(provinceData) {
    const myEchart = echarts.init(document.querySelector('#map'))
    const dataList = [
      { name: '南海诸岛', value: 0 },
      { name: '北京', value: 0 },
      { name: '天津', value: 0 },
      { name: '上海', value: 0 },
      { name: '重庆', value: 0 },
      { name: '河北', value: 0 },
      { name: '河南', value: 0 },
      { name: '云南', value: 0 },
      { name: '辽宁', value: 0 },
      { name: '黑龙江', value: 0 },
      { name: '湖南', value: 0 },
      { name: '安徽', value: 0 },
      { name: '山东', value: 0 },
      { name: '新疆', value: 0 },
      { name: '江苏', value: 0 },
      { name: '浙江', value: 0 },
      { name: '江西', value: 0 },
      { name: '湖北', value: 0 },
      { name: '广西', value: 0 },
      { name: '甘肃', value: 0 },
      { name: '山西', value: 0 },
      { name: '内蒙古', value: 0 },
      { name: '陕西', value: 0 },
      { name: '吉林', value: 0 },
      { name: '福建', value: 0 },
      { name: '贵州', value: 0 },
      { name: '广东', value: 0 },
      { name: '青海', value: 0 },
      { name: '西藏', value: 0 },
      { name: '四川', value: 0 },
      { name: '宁夏', value: 0 },
      { name: '海南', value: 0 },
      { name: '台湾', value: 0 },
      { name: '香港', value: 0 },
      { name: '澳门', value: 0 },
    ]
    dataList.forEach(item => {
      const res = provinceData.find(v => {
        return v.name.includes(item.name)
      })
      // console.log(res)
      // 数据赋值
      if (res !== undefined) {
        item.value = res.value
      }
    })
    const option = {
      title: {
        text: '籍贯分布',
        top: 10,
        left: 10,
        textStyle: {
          fontSize: 16,
        },
      },
      tooltip: {
        trigger: 'item',
        formatter: '{b}: {c} 位学员',
        borderColor: 'transparent',
        backgroundColor: 'rgba(0,0,0,0.5)',
        textStyle: {
          color: '#fff',
        },
      },
      visualMap: {
        min: 0,
        max: 6,
        left: 'left',
        bottom: '20',
        text: ['6', '0'],
        inRange: {
          color: ['#ffffff', '#0075F0'],
        },
        show: true,
        left: 40,
      },
      geo: {
        map: 'china',
        roam: false,
        zoom: 1.0,
        label: {
          normal: {
            show: true,
            fontSize: '10',
            color: 'rgba(0,0,0,0.7)',
          },
        },
        itemStyle: {
          normal: {
            borderColor: 'rgba(0, 0, 0, 0.2)',
            color: '#e0ffff',
          },
          emphasis: {
            areaColor: '#34D39A',
            shadowOffsetX: 0,
            shadowOffsetY: 0,
            shadowBlur: 20,
            borderWidth: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
      series: [
        {
          name: '籍贯分布',
          type: 'map',
          geoIndex: 0,
          data: dataList,
        },
      ],
    }
    myEchart.setOption(option)
  }